/**
 * JAVACC DEMO 1.0
 */
package com.apache.info.service.plugins;

import com.alibaba.druid.pool.DruidDataSource;
import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.datasource.ConnctionData;
import com.apache.database.datasource.DynamicDataSource;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.manager.InfoReleaseManager;
import com.apache.info.util.MessageUtil;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * description:  验证表是否存在
 *
 * @author Hou Dayu 创建时间：2016-8-28
 */
public class ExistTablePluiginImpl implements PluginConnector {
	protected Logger log = Logger.getLogger(getClass());
	private InfoReleaseManager infoReleaseManager;

	/**
	 * TODO 验证表是否存在插件实现.
	 *
	 * @see PluginConnector#execute(ParamsVo)
	 */
	public Object execute(ParamsVo vo) throws Exception {
		String tableName = String.valueOf(vo.getParams("tableName"));
		long tbNum = 0;
		if (Validator.isNotNull(tableName)) {
			PubSortInfo sort = (PubSortInfo) vo.getParams("sort");
			String sysName = StrUtil.doNull(sort.getSysName(), "plateform");
			DruidDataSource bds = DynamicDataSource.getInstance().getDataSource(sysName);//ConnctionData.getInstance().getDataSource(sysName);
			//			if (null == bds) {
			//				bds = ConnctionData.getInstance().getDataSource("plateform");
			//			}
			log.info("start create table [" + tableName + "] by  data source ["+sysName+"]");
			log.info("table check is exist jdbc url -> " + bds.getUrl());
			String dbType = MessageUtil.getDatabastType(sysName, bds.getDriverClassName());
			//            ParamsVo pvo = new ParamsVo();
			//            pvo.setParams("dyncSql", getSqlStr(tableName, dbType));
			//            pvo.setMethodKey("ExistTable");
			//            tbNum = infoReleaseManager.countInfo(pvo);
			boolean mark =false;
			try {
				Connection con = bds.getConnection();
				tbNum = ConnctionData.getInstance().selectCount(con, getSqlStr(tableName, dbType, sysName));
			}catch (SQLException e){
				DynamicDataSource.getInstance().removeDataSource(sysName);
				return false;
			}
		}
		return tbNum;
	}

	private String getSqlStr(String tableName, String dbType, String sysName) {
		//        String dbType = ConfigUtil.getInstance().getValueByKey("jdbc.properties", "database_type");
		//mysql,oracle,sqlserver,db2
		String sql = "";
		if ("oracle".equals(dbType)) {
			sql = "select count(*) from tabs where table_name ='" + tableName.toUpperCase() + "' and tablespace_name='"
					+ sysName + "'";
		} else if ("sqlserver".equals(dbType)) {
			sql = "select count(1) from sys.systables where LCASE(tablename)='pub_metadata'" + tableName + "'";
		} else if ("mysql".equals(dbType)) {
			sql = "select count(1) from INFORMATION_SCHEMA.TABLES where TABLE_NAME='" + tableName
					+ "'  and TABLE_SCHEMA='" + sysName + "'";
		}
		return sql;
	}

	public void setInfoReleaseManager(InfoReleaseManager infoReleaseManager) {
		this.infoReleaseManager = infoReleaseManager;
	}

}
